using System;
using System.Drawing;
using System.Windows.Forms;
using SPTraceView.Model;
using System.Net;

namespace SPTraceView
{
    internal partial class frmFarmSettings : Form
    {

        private TraceSettings.FarmRoleSettings m_FarmSettings;
        private bool m_SimpleMode = true;

        internal frmFarmSettings(Icon icon, TraceSettings.FarmRoleSettings farmSettings)
        {
            InitializeComponent();

            this.Icon = icon;
            m_FarmSettings = farmSettings;

            rbProvider.Checked = farmSettings.m_FarmRole == TraceViewFarmRole.Server;
            rbSimpleProvider.Checked = farmSettings.m_FarmRole == TraceViewFarmRole.Server;

            rbFarmClient.Checked = farmSettings.m_FarmRole == TraceViewFarmRole.Client;
            rbSimpleServer.Checked = farmSettings.m_FarmRole == TraceViewFarmRole.Client;

            rbStandAlone.Checked = farmSettings.m_FarmRole == TraceViewFarmRole.StandAlone;
            rbSimpleStandalone.Checked = farmSettings.m_FarmRole == TraceViewFarmRole.StandAlone;

            rbBroadcastSpecific.Checked = !farmSettings.m_UseMulticastGroup;
            rbOtherFarm.Checked = farmSettings.m_UseForeignFarm;
            rbTraceLocalFarm.Checked = !farmSettings.m_UseForeignFarm;
            tbxOtherFarmID.Text = Guid.Empty.Equals(farmSettings.m_ForeignFarmID) ? string.Empty : farmSettings.m_ForeignFarmID.ToString("D").ToUpper();
            tbxLocalFarmID.Text = SPSafeHelper.LocalFarmId.ToString("D").ToUpper();
            tbxBroadcastAddress.Text = farmSettings.m_BroadcastAddress;
            rbServerMulticast.Checked = farmSettings.m_ServerUseMulticast;
            rbServerBroadcast.Checked = !farmSettings.m_ServerUseMulticast;


            lblPort1.Text = UDPHelper.SP_TRACE_VIEW_PORT.ToString();
            lblPort2.Text = UDPHelper.SP_TRACE_VIEW_PORT.ToString();

            gbxFramRoleAdvanced.Left = 12;
            gbxFramRoleSimple.Left = 12;

            m_SimpleMode = true;
            SetAdvancedMode(false);
        }

        private void SetEnabledDisabled()
        {
            pnlProvider.Enabled = rbProvider.Checked;
            pnlReceiver.Enabled = rbFarmClient.Checked;
            tbxBroadcastAddress.Enabled = rbBroadcastSpecific.Checked;
            tbxOtherFarmID.Enabled = rbOtherFarm.Checked;
        }

        private void rbStandAlone_CheckedChanged(object sender, EventArgs e)
        {
            SetEnabledDisabled();
        }

        private void rbProvider_CheckedChanged(object sender, EventArgs e)
        {
            SetEnabledDisabled();
        }

        private void cbxBroadcastSpecific_CheckedChanged(object sender, EventArgs e)
        {
            SetEnabledDisabled();
        }

        private void rbFarmClient_CheckedChanged(object sender, EventArgs e)
        {
            SetEnabledDisabled();
        }

        private void rbTraceLocalFarm_CheckedChanged(object sender, EventArgs e)
        {
            SetEnabledDisabled();
        }

        private void rbOtherFarm_CheckedChanged(object sender, EventArgs e)
        {
            SetEnabledDisabled();
        }


        private void rbBroadcastSpecific_CheckedChanged(object sender, EventArgs e)
        {
            SetEnabledDisabled();
        }

        private void frmFarmSettings_Load(object sender, EventArgs e)
        {
            SetEnabledDisabled();

            btnCancel.Select();
            btnCancel.Focus();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (rbProvider.Checked)
            {
                if (rbBroadcastSpecific.Checked)
                {
                    if (tbxBroadcastAddress.Text == string.Empty)
                    {
                        MessageBox.Show(this, "Please specify an IP address.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        tbxBroadcastAddress.SelectAll();
                        tbxBroadcastAddress.Select();
                        tbxBroadcastAddress.Focus();
                        return;
                    }

                    try
                    {
                        IPAddress broadcastAddress = IPAddress.Parse(tbxBroadcastAddress.Text.Trim());
                        byte[] addressBytes = broadcastAddress.GetAddressBytes();
                        if (addressBytes[3] != 255)
                        {
                            if (MessageBox.Show(
                                    this,
                                    string.Format("Broadcast IP addresses usually look like x.x.x.255 \r\n\r\nPlease confirm that {0} is a valid broadcast IP address?", broadcastAddress.ToString()),
                                    "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                            {
                                tbxBroadcastAddress.SelectAll();
                                tbxBroadcastAddress.Select();
                                tbxBroadcastAddress.Focus();
                                return;
                            }
                        }

                        m_FarmSettings.m_BroadcastAddress = broadcastAddress.ToString();
                        
                    }
                    catch (FormatException)
                    {
                        MessageBox.Show(this, "The specified IP address is invalid.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        tbxBroadcastAddress.SelectAll();
                        tbxBroadcastAddress.Select();
                        tbxBroadcastAddress.Focus();
                        return;
                    }
                }

                m_FarmSettings.m_UseMulticastGroup = !rbBroadcastSpecific.Checked;
                m_FarmSettings.m_FarmRole = TraceViewFarmRole.Server;
            }
            else if (rbFarmClient.Checked)
            {
                if (rbOtherFarm.Checked)
                {
                    if (tbxOtherFarmID.Text == string.Empty)
                    {
                        MessageBox.Show(this, "Please specify a farm ID.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        tbxOtherFarmID.SelectAll();
                        tbxOtherFarmID.Select();
                        tbxOtherFarmID.Focus();
                        return;
                    }

                    Guid farmID = Guid.Empty;

                    try
                    {
                        farmID = new Guid(tbxOtherFarmID.Text.Trim());
                    }
                    catch (FormatException)
                    {
                        MessageBox.Show(this, "The specified farm ID is invalid.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        tbxOtherFarmID.SelectAll();
                        tbxOtherFarmID.Select();
                        tbxOtherFarmID.Focus();
                        return;
                    }

                    m_FarmSettings.m_ForeignFarmID = farmID;
                }

                m_FarmSettings.m_ServerUseMulticast = rbServerMulticast.Checked;
                m_FarmSettings.m_UseForeignFarm = rbOtherFarm.Checked;
                m_FarmSettings.m_FarmRole = TraceViewFarmRole.Client;
            }
            else if (rbStandAlone.Checked)
            {
                m_FarmSettings.m_FarmRole = TraceViewFarmRole.StandAlone;
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnAdvanced_Click(object sender, EventArgs e)
        {
            m_SimpleMode = !m_SimpleMode;

            if (m_SimpleMode)
            {
                rbSimpleServer.Checked = rbFarmClient.Checked;
                rbSimpleProvider.Checked = rbProvider.Checked;
                rbSimpleStandalone.Checked = rbStandAlone.Checked;
            }

            SetAdvancedMode(!m_SimpleMode);
        }

        private void SetAdvancedMode(bool advanced)
        {
            if (advanced)
            {
                gbxFramRoleAdvanced.Visible = true;
                gbxFramRoleAdvanced.BringToFront();
                gbxFramRoleSimple.Visible = false;
                this.Width = 550;
                this.Height = 425 - 6;
                btnAdvanced.Text = "&Simple";
            }
            else
            {
                gbxFramRoleSimple.Visible = true;
                gbxFramRoleSimple.BringToFront();
                gbxFramRoleAdvanced.Visible = false;
                this.Width = 447;
                this.Height = 305 - 6;
                btnAdvanced.Text = "&Advanced";

                SyncSimpleToAdvanced();
            }
        }

        private void SyncSimpleToAdvanced()
        {
            if (rbSimpleStandalone.Checked)
            {
                rbStandAlone.Checked = true;
            }

            if (rbSimpleProvider.Checked)
            {
                rbProvider.Checked = true;
                rbMulticast.Checked = true;
            }

            if (rbSimpleServer.Checked)
            {
                rbFarmClient.Checked = true;
                rbTraceLocalFarm.Checked = true;
                rbServerMulticast.Checked = true;
            }
        }

        private void rbSimpleStandalone_CheckedChanged(object sender, EventArgs e)
        {
            SyncSimpleToAdvanced();
        }

        private void rbSimpleProvider_CheckedChanged(object sender, EventArgs e)
        {
            SyncSimpleToAdvanced();
        }

        private void rbSimpleServer_CheckedChanged(object sender, EventArgs e)
        {
            SyncSimpleToAdvanced();
        }
    }
}